<?php
/**
 * @file trackback.php 群发TrackBack
 * @package TrackBack
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-2-26 下午03:54:26 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2009 MiFunny China Inc. (http://mifunny.info)
 */
$submit = $_POST['submit'];
$blog_name = $_POST['blog_name'];
$title = $_POST['title'];
$excerpt = $_POST['excerpt'];
$url = trim($_POST['url']);
$trackback_url = $_POST['trackback_url'];
$charset = 'UTF-8';
$follow = $_POST['follow'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Send TrackBack | 水草树</title>
<meta name="robots" content="nofollow" />
<style type="text/css">
#wrapper{ width:800px;margin:0 auto;}
#header{height:18px;text-align:right;font-size:12px;position:relative;}
#content{font-size:14px;}
#footer {text-align:center;margin:75px 0 25px 0;position:relative;}
.box{border:2px solid #999; background-color:#ffffce;margin:10px;padding:5px;}
.error{color:red;}
.success{color:blue;}
.old{color:gray;}
#content input{margin:5px;}
</style>
</head>
<body>
<div id="wrapper">
	<div id="header">
		<p><a href="checkurl.php">CHECK URL</a> | <a href="trackback.php">SEND TRACKBACK</a></p>
	</div>

<div id="content">
<?php 
// echo get_include_path(). PATH_SEPARATOR.dirname($_SERVER['SCRIPT_FILENAME']).'/pear/php';
if (!empty($submit)){
	set_include_path( get_include_path(). PATH_SEPARATOR.dirname($_SERVER['SCRIPT_FILENAME']).'/pear/php' );
	// include class
	include "Services/Trackback.php";
	
	if ($follow != 1) $follow = 0;
	
	// Trackback 链接s
	$tb_urls = preg_split('/[\n\r\t]/', $trackback_url, -1, PREG_SPLIT_NO_EMPTY);
	
	// 摘要
	$excerpt = substr($excerpt,0,230);
	$excerpt = '...'.utf8_trim($excerpt);
	
	// 连接 SQLite 
	try {
		$dbh = new PDO("sqlite:SQLTrackback.db", null, null, array(PDO::ATTR_PERSISTENT => true));
	}catch (PDOException $ex){
		die("[Error]: ".$ex->getMessage().'<br />\n');
	}
	// Useless but carefully is better, set encoding.
	// Sqlite always use utf-8 internaly, UTF-8 is also it's default value.
	$dbh->query('PRAGMA encoding = "UTF-8"');
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	?>
<div class="box">
	<?php 
	// 循环
	for ($i=0; $i<count($tb_urls); $i++){
		$tb_url = trim($tb_urls[$i]);
		$sqlSelect = "SELECT id, url FROM tb WHERE url = \"".$tb_urls[$i]."\" ;";
		$result = $dbh->query($sqlSelect);
		$row = $result->fetch(PDO::FETCH_BOTH);
		if (empty($row[0])){ //未含有URL
			$trackbackData = array(	'title' => $title,
					'excerpt' => $excerpt,
					'url' => $url,	
					'blog_name' => $blog_name,
					'charset' => $charset,
					'trackback_url' => $tb_url );
			// initialize new instance
			$trackback = new Services_Trackback();
			foreach ($trackbackData as $k => $v) {
  				$trackback->set($k, $v);
			}
			// send trackback
			$ret = $trackback->send();
			if (PEAR::isError($ret)) {
				?>
	ERROR URL: <span class="error"><?php echo $tb_url; ?></span><br />
				<?php 
			} else {
				$sqlInsert = "INSERT INTO tb VALUES(null, \"".$tb_url."\", ".$follow.", julianday('now') )";
	  			if ($dbh->exec($sqlInsert) != 1){
					?>
	URL submit Error: <span class="error"><?php echo $tb_url; ?></span><br />
					<?php 
				}else {
					?>
	Submit Successfully: <span class="success"><?php echo $tb_url; ?></span><br />
					<?php
				}
			} //END Send trackback
		}else{ //if url empty
		?>
	Old URL: <span class="old"><?php echo $tb_url; ?></span><br />
		<?php 
		} //END trackback 地址重复
	} //END for 循环发送 trackback
	?>
</div><!-- .box -->
	<?php 
} //END submit
/* 以下函数来自Wordpress中文工具箱 http://yanfeng.org/blog/wordpress/kit */
function utf8_trim($str) {
	//$len = strlen($str);
	for ($i=strlen($str)-1; $i>=0; $i-=1){
		$hex .= ' '.ord($str[$i]);
		$ch = ord($str[$i]);
        if (($ch & 128)==0) return(substr($str,0,$i));
		if (($ch & 192)==192) return(substr($str,0,$i));
	}
	return($str.$hex);
}
?>
<p>输入你要发送的 TRACKBACK 信息<br />如果 TRACKBACK_URL 重复则提示，发送成功则入库！</p>
<form method="post" action="trackback.php" onsubmit="return sendtb(this);">
<b>TrackBack Ping URL</b>: <input type="checkbox" name="follow" value="1" checked="checked" /> Follow (1)
<br /><textarea name="trackback_url" rows="6" cols="60"></textarea>
<br />

<br /><b>Weblog's Name</b>: <input name="blog_name" size="80" />
<br /><b>Post Title</b>: <input name="title" size="80" />
<br />Enter the name of your blog. Maximum of 80 characters.<br />
<br /><b>Permalink URL</b>: <input name="url" size="80" value="http://" />
<br />This is the permanent URL to your entry on <em>your</em> blog, which is referencing the post you're pinging. On most blogging systems the permanent link is often underneath the entry denoted by "link" or "permanent link" text. This must be a complete URL, so please include the http://<br />
<br /><b>Excerpt</b>:
<br /><textarea name="excerpt" rows="6" cols="60"></textarea>
<br />This is an excerpt from your post. Max of 250 characters (if longer, the text will be cut shorted and have a "..." added to the end).  HTML is allowed, but make sure not to include special characters that will render incorrectly (use ASCII or HTML entities).
<br />
<p>
	<input name="submit" type="submit" value="Submit Trackback" />
	<input type="reset" value="Clear Form" name="reset" />
</p>

<script language="Javascript" type="text/javascript">
<!--
  function sendtb(theForm)
  {
    if (document.layers == null)
    {
      var error = "";
      error = (theForm.trackback_url.value == "") ? "Please enter a Trackback Ping URL\n" : "";
      error = (theForm.url.value == "") ? "Please enter a URL to link to\n" : "";
      error = (theForm.blog_name.value == "") ? "Please enter an Author/Site Name\n" : "";
      error = (theForm.title.value == "") ? "Please enter an Article/Entry Title\n" : "";
      if (error != "")
      {
        alert(error);
        return false;
      }
      theForm.action = theForm.tb_url.value;
      return true;
    }
  }
// -->
</script>
</form>
<div><!-- #content -->
<div id="footer">
	<p>Copyright &copy; 2008 - 2009, <a href="http://mifunny.info/">水草树</a>, All right reserved.</p>
</div>

</div><!-- #wrapper -->
</body>
</html>